< previous page page_580 next page >

Page 580
Listing 26.1 shows the use of the FindFirst method to look for records based on user-defined criteria.
Listing 26.1 FINDTEST.TXTUsing FindFirst to Look for a Record
Private Sub cmdFindFirst_Click()
    Dim sCriteria As String
    Dim sBkmark As String

    'Build the criteria string
    sCriteria = "title like '*" & txtFind.Text & "*'"

    'Remember where we were in case Find fails
    sBkmark = rsTitles.Bookmark

    'Execute the Find
    rsTitles.FindFirst sCriteria

    'Check for success
    If rsTitles.NoMatch Then
        MsgBox "Record not found!"
        'If failed, return to last good record
        rsTitles.Bookmark = sBkmark
    End If

    'Fill the text boxes
    ShowFields

End Sub
tip.gif
In many cases, re-creating a dynaset using the search criteria is faster than using the Find methods to process all matching records. You can also create a second filtered dynaset from the first dynaset by using the search criteria as the filter condition. The best technique depends on the amount of data, size of each record, as well as other factors. Try different approaches with your data to see what's best for a given situation.

d6ef40187df115e88326dbc94f09ab9a.gif d6ef40187df115e88326dbc94f09ab9a.gif
Cannot bind . . . Error
d6ef40187df115e88326dbc94f09ab9a.gif d6ef40187df115e88326dbc94f09ab9a.gif
When you use variables as the value to be compared to, you might encounter the error Cannot bind name item when you run the program. When the field and the variable you are comparing are string (or text) variables, surround the variable name with single quotation marks ('), as shown in the following sample code:
d6ef40187df115e88326dbc94f09ab9a.gif d6ef40187df115e88326dbc94f09ab9a.gif
Dim sFindCrit As String, sFindStr As String
sFindStr = "Smith"
sFindCrit = "Lastname = '" & FindStr & "'"
rsTest.FindFirst FindCrit
d6ef40187df115e88326dbc94f09ab9a.gif d6ef40187df115e88326dbc94f09ab9a.gif
For the sake of readability, you can also assign the single quotation mark to a constant and use that constant in your code.
d6ef40187df115e88326dbc94f09ab9a.gif d6ef40187df115e88326dbc94f09ab9a.gif
In the same manner, surround a date variable with the pound symbol (#) to compare it to a date field. You don't need to include any additional symbols when comparing numbers.

 
< previous page page_580 next page >